package ca.camosun.proximity;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AlertVibrate extends Activity implements OnClickListener {
	
	NotificationManager warningNotification;
	private static final int NOTIFICATION_ID = 1;
	private Button backButton;
	TextView message;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alert);
		
		backButton = (Button) findViewById(R.id.endAlertButton);
		backButton.setOnClickListener(this);
	}
	
	private Vibrator alertVibrator;
	private long[] pattern = { 0, 200, 500 };
	
	public AlertVibrate(Context inContext) {
		alertVibrator = (Vibrator) inContext
                .getSystemService(Context.VIBRATOR_SERVICE);
	}
	
	
	void vibrate() {
		alertVibrator.vibrate(pattern , 0);
	}
	
	
	@Override
	public void onClick(View v) {
		if (v.equals(backButton)) {
			alertVibrator.cancel();
			finish();
			Intent intent = new Intent(null, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
		}
	}
	
	public void targetLeft(Person target) {
			
		Notification.Builder noteWarning =
		        new Notification.Builder(this)
		        //.setSmallIcon(R.drawable.notification_icon)
		        .setContentTitle("Target has left your range")
		        .setContentText(target.getFirstName() + " has left your range!");
		
		Intent alert = new Intent(this, AlertVibrate.class);
		
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		
		stackBuilder.addParentStack(AlertVibrate.class);
		
		stackBuilder.addNextIntent(alert);
		PendingIntent Pendingalarm =
		        stackBuilder.getPendingIntent(
		            0,
		            PendingIntent.FLAG_UPDATE_CURRENT
		        );
		
		noteWarning.setContentIntent(Pendingalarm);
		
		warningNotification = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		
		warningNotification.notify( 0, noteWarning.build());
		
	}
	
	public void targetEnter(Person inTarget) {
		
		Notification.Builder noteWarning =
		        new Notification.Builder(this)
		        //.setSmallIcon(R.drawable.notification_icon)
		        .setContentTitle("Target has entered your range")
		        .setContentText(inTarget.getFirstName() + " has left your range!");
		
		Intent alert = new Intent(this, AlertVibrate.class);
		
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		
		stackBuilder.addParentStack(AlertVibrate.class);
		
		stackBuilder.addNextIntent(alert);
		PendingIntent Pendingalarm =
		        stackBuilder.getPendingIntent(
		            0,
		            PendingIntent.FLAG_UPDATE_CURRENT
		        );
		
		noteWarning.setContentIntent(Pendingalarm);
		
		warningNotification = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		
		warningNotification.notify( 0, noteWarning.build());
		
	}
	
}
